package com.andova.toolkit;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Build;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * Created by Administrator on 2017-07-20.
 *
 * @author kzaxil
 * @since 1.0.0
 */
public class Util {
    public static int width(Resources resources) {
        DisplayMetrics dm = resources.getDisplayMetrics();
        return dm.widthPixels;
    }

    public static void text(View view, String text) {
        if (TextUtils.isEmpty(text)) return;
        if (!(view instanceof TextView)) return;
        ((TextView) view).setText(text);
    }

    public static String text(EditText editText) {
        if (editText == null) return "";
        return editText.getText().toString().trim();
    }

    public static String text(View view) {
        if (view instanceof EditText) {
            return ((EditText) view).getText().toString().trim();
        }
        return "";
    }

    public static boolean empty(View view) {
        if (view instanceof EditText) {
            return TextUtils.isEmpty(((EditText) view).getText().toString().trim());
        }
        if (view instanceof TextView) {
            return TextUtils.isEmpty(((TextView) view).getText().toString().trim());
        }
        return true;
    }

    /**
     * 将字符串传参转为int类型，若转换出错，则返回0
     */
    public static int parseInt(String value) {
        if (TextUtils.isEmpty(value)) return 0;
        try {
            return Integer.parseInt(value);
        } catch (NumberFormatException e) {
            return 0;
        }
    }

    public static <T> T convert(Object obj) {
        try {
            return (T) obj;
        } catch (ClassCastException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String convertToDate(String dateTime) {
        DateFormat format = new SimpleDateFormat("yyy-MM-dd HH:mm:ss", Locale.getDefault());
        try {
            Date date = format.parse(dateTime);
            return new SimpleDateFormat("yyy-MM-dd", Locale.getDefault()).format(date);
        } catch (IllegalArgumentException | ParseException e) {
            return dateTime;
        }
    }

    /**
     * 将字符串集合的所有字符串通过给定的分隔字符串进行合并
     */
    public static String merge(List<String> list, String divider) {
        if (list == null || list.size() == 0) return "";
        if (TextUtils.isEmpty(divider)) {
            divider = " ";
        }
        StringBuilder sb = new StringBuilder();
        for (String content : list) {
            sb.append(content);
            sb.append(divider);
        }
        if (sb.length() > 0) {
            sb.deleteCharAt(sb.length() - 1);
        }
        return sb.toString();
    }

    public static String dateTime() {
        DateFormat format = new SimpleDateFormat("yyy-MM-dd HH:mm:ss", Locale.getDefault());
        return format.format(new Date());
    }

    /**
     * 判断是否是电话号码
     */
    public static boolean isTelNumber(String mobiles) {
        String telRegex = "[1][3456789]\\d{9}";
        return !TextUtils.isEmpty(mobiles) && mobiles.matches(telRegex);
    }

    /**
     * 沉侵式状态栏和底部导航栏（5.0及以上），调用该方法，默认控件是会占用状态栏和导航栏的空间的，
     * 若不想这种情况发生，需要在对应布局XML文件的根元素设置android:fitsSystemWindows="true"属性
     *
     * @param activity              沉侵式页面，需要注意的是，该方法需要在{@link Activity#setContentView(int)}之前调用
     * @param translucentStatus     是否透明顶部状态栏
     * @param translucentNavigation 是否透明系统自带底部导航栏
     */
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    protected void translucent(Activity activity, boolean translucentStatus, boolean translucentNavigation) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = activity.getWindow();
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
            if (translucentStatus) window.setStatusBarColor(Color.TRANSPARENT);
            if (translucentNavigation) window.setNavigationBarColor(Color.TRANSPARENT);
        }
    }
}
